package com.xz;

import com.xz.printer.BinaryTrees;

import java.util.Comparator;

/**
 * @author XZ
 * @create 2021-06-06 20:57
 */
public class Main {

    private static class PersonComparator implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e1.getAge()-e2.getAge();
        }
    }

    private static class PersonComparator2 implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e2.getAge()-e1.getAge();
        }
    }

    static void test1() {
        Integer[] data = new Integer[]{
                7,4,9,2,5,8,11,3,12,1
        };
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
    }

    static void test2() {
        Integer[] data = new Integer[]{
                7,4,9,2,5,8,11,3,12,1
        };
        BinarySearchTree<Person> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(new Person(data[i]));
        }
        BinaryTrees.println(bst);

        BinarySearchTree<Person> bst2 = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge()-o1.getAge();
            }
        });


        for (int i = 0; i < data.length; i++) {
            bst2.add(new Person(data[i]));
        }
        BinaryTrees.println(bst2);
    }


    static void test3() {
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 30; i++) {
            bst.add((int)(Math.random()*100));
        }
        BinaryTrees.println(bst);
    }

    static void test4() {
        BinarySearchTree<Person> bst = new BinarySearchTree<>();
        bst.add(new Person(10,"jack"));
        bst.add(new Person(12,"rose"));
        bst.add(new Person(6,"jim"));
        bst.add(new Person(10,"micheal"));

        BinaryTrees.println(bst);
    }

    static void test6() {
        Integer[] data = new Integer[]{
                7,4,2,1,3,5,9,8,11,10,12,0
        };
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);


//        bst.levelOrder(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public void visit(Integer element) {
//                System.out.print("_"+element+"_ ");
//            }
//        });

//        bst.inorder(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public void visit(Integer element) {
//                System.out.print("_"+element+"_ ");
//            }
//        });
        bst.postorder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print("_"+element+"_ ");
                return false;
            }
        });

        System.out.println();
        bst.preorder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print("_"+element+"_ ");
                return element==2? true:false;
            }
        });
        System.out.println();
        bst.levelOrder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print("_"+element+"_ ");
                return element==2? true:false;
            }
        });
        System.out.println();
        System.out.println();
        System.out.println();

        System.out.println(bst.height1());
        System.out.println(bst.height2());

    }


    static void test7() {
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 20; i++) {
            bst.add((int)(Math.random()*100));
        }
        BinaryTrees.println(bst);
        System.out.println(bst.isComplete());
    }

    static void test8() {
        Integer[] data = new Integer[]{
                7,4,9,2,5,8,11,3,12,1
        };
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
        bst.remove(7);
        BinaryTrees.println(bst);
    }

    public static void main(String[] args) {
        test8();

//        BinarySearchTree<Person> bst2 = new BinarySearchTree<>(new Comparator<Person>() {
//            @Override
//            public int compare(Person o1, Person o2) {
//                return o1.getAge()- o2.getAge();
//            }
//        });//匿名类
//
//        bst2.add(new Person(12));
//        bst2.add(new Person(15));
    }
}